/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.beans.dish;

import ecom.beans.store.*;


import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tian
 */
@Stateless
public class DishEntityFacade implements DishEntityFacadeRemote {

    @PersistenceContext
    private EntityManager em;

    public List<DishEntity> getAllDishes() {

        Query getStore;

        try {

            getStore = em.createNamedQuery("findAllDishes");


        } catch (Exception e) {

            getStore = null;
        }

        return (List<DishEntity>) getStore.getResultList();
    }
    
    public DishEntity getDishById(int id){
        DishEntity dish = null;
        Long longid = new Long(id);

        try {

            dish = (DishEntity) em.createNamedQuery("findDishById").setParameter("id", longid).getSingleResult();

        } catch (Exception e) {

            dish = null;

        }

        return dish;
    }

    //the dish is showed only if it is in stock
    //we should have another method for the admin to show all dishes
    public List<DishEntity> getAllDishesForCustomer() {

        Query getStore;
        List<DishEntity> prodList = null;
        try {

            getStore = em.createNamedQuery("findAllDishes");
            prodList = (List<DishEntity>) getStore.getResultList();

            for (int i = prodList.size() - 1; i >= 0; i--) {

                if (prodList.get(i).getQuantity() < 1) {

                    prodList.remove(i);

                }
            }

        } catch (Exception e) {

            prodList = null;
        }

        return prodList;
    }

     public List<DishEntity> getAllDishesForResto(int restoId) {


          List<DishEntity> temp;
          
        try {

            temp = (List<DishEntity>) em.createNamedQuery("findDishesByStoreId").setParameter("storeId", new Long(restoId)).getResultList();

            
        } catch (Exception e) {

            temp = null;
        }

        return temp;
    }
     
    public List<DishEntity> removeAllDishesByRestoId(int restoId) {
         List<DishEntity> list;

        try {

            list = getAllDishesForResto(restoId);

            if (list != null && list.size() > 0) {

                for (int i = 0; i < list.size(); i++) {

                    em.remove(list.get(i));
                }
            } else {
                list = null;
            }
        } catch (Exception e) {

            list = null;
        }

        return list;
    }

    public boolean addDish(String name, double price, int quantity) {

        List<DishEntity> dishList = null;

        /*   dishList = (List<DishEntity>) em.createNamedQuery("findDishByName").setParameter("dishName", name).getResultList();
        
        if (dishList.size() == 0 || dishList == null) {
         */
        DishEntity newDish = new DishEntity();

        newDish.setDishName(name);

        newDish.setPrice(price);

        newDish.setQuantity(quantity);

        em.persist(newDish);

        return true;

    /**      } else {
    return false;
    } */
    }

    public DishEntity addDish(String name, double price, int quantity, String dishType, int calorieNumber,StoreEntity store) {

        List<DishEntity> dishList = null;

        dishList = (List<DishEntity>) em.createNamedQuery("findDishByName").setParameter("dishName", name).getResultList();

        if (dishList.size() == 0 || dishList == null) {

            DishEntity newDish = new DishEntity();

            newDish.setDishName(name);

            newDish.setPrice(price);

            newDish.setQuantity(quantity);
			
			newDish.setCalorieNumber(calorieNumber);

            newDish.setStore(store);

            newDish.setDishType(dishType);

            em.persist(newDish);

            return newDish;

        } else {
            return null;
        }

    }

    public List<DishEntity> removeAllDish() {

        List<DishEntity> list;

        try {

            list = getAllDishes();

            if (list != null && list.size() > 0) {

                for (int i = 0; i < list.size(); i++) {

                    em.remove(list.get(i));
                }
            } else {
                list = null;
            }
        } catch (Exception e) {

            list = null;
        }

        return list;
    // not sure yet

    }

    public DishEntity removeDishById(int id) {

        DishEntity removedDish;

        try {

            removedDish = getDishById(id);

            //if (removedDish != null) {

            em.remove(removedDish);
           
        } catch (Exception e) {

            removedDish = null;
        }

        return removedDish;
    // not sure yet

    }

    public void updateDish(DishEntity produit) {

        em.merge(produit);

    }
	
	
	public List<DishEntity> removeAllDishesByRestoId(Long restoId) {
	
        List<DishEntity> list;

        try {

            list = getAllDishesForResto(restoId);

            if (list != null && list.size() > 0) {

                for (int i = 0; i < list.size(); i++) {

                    em.remove(list.get(i));
                }
            } else {
                list = null;
            }
        } catch (Exception e) {

            list = null;
        }

        return list;
    }

	
	public List<DishEntity> getAllDishesForResto(Long restoId) {


          List<DishEntity> temp;
          
        try {

            temp = (List<DishEntity>) em.createNamedQuery("findDishesByStoreId").setParameter("storeId", restoId).getResultList();

            
        } catch (Exception e) {

            temp = null;
        }

        return temp;
    }
	
	
	 public DishEntity getDishByNameAndStoreId(String name, int storeId) {

        DishEntity dish = null;

        try {

            dish = (DishEntity) em.createNamedQuery("findDishByStoreIdAndName").setParameter("dishName", name).setParameter("storeId", storeId).getSingleResult();

        } catch (Exception e) {

            dish = null;

        }

        return dish;

    }


	
}
